<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/18
 * Time: 18:27
 */

namespace App\Http\Classes\IM;


use App\Http\Classes\Common;

class IMCheckSumBuilder
{
    private $nonce;
    private $curTime;
    private $appSecret;

    public function __construct($appSecret)
    {
        $this->appSecret = $appSecret;
        $this->getTime();
        $this->getNonce();
        return $this->getCheckSum();
    }

    private function getTime(){
        $this->curTime = time();
    }

    private function getNonce(){
        $this->nonce = Common::getRand();
    }

    public function getCheckSum(){
        $value = $this->appSecret . $this->nonce . $this->curTime;
        if($value == null){
            return null;
        }
        else{
            return hash('sha1', $value);
        }
        //return self::encode("sha1", $appSecret + $nonce + $curTime);
    }

    public static function getMD5($requestBody){
        return hash('md5',$requestBody);
        //return self::encode('md5', $requestBody);
    }
//
//    private static function encode($algorithm,$value){
//        if ($value == null) {
//            return null;
//        }
//        try {
//            $messageDigest = MessageDigest.getInstance($algorithm);
//            messageDigest.update(value.getBytes());
//            return self::getFormattedText(messageDigest.digest());
//        } catch (\Exception $exception) {
//            throw new $exception;
//        }
//    }

//    private static function getFormattedText($bytes){
//        $len = count($bytes);
//
//    }
}